# -*- coding: utf-8 -*-
# File : sql_init.py
# Author: taoyahui
# Date : 2022/5/30

# 这是博客系统数据库的初始化操作
import sqlite3

# 第一步，创建连接
conn = sqlite3.connect('blog.db')
# 创建游标
cursor = conn.cursor()

try:
    # 创建blogs
    cursor.execute("DROP TABLE IF EXISTS blogs")
    cursor.execute("CREATE TABLE blogs(id INTEGER PRIMARY KEY AUTOINCREMENT, title VARCHAR(40), content TEXT, create_time datetime)")
    cursor.execute("insert into blogs values (null, '第一个博客', '博客内容', '2018-08-06')")
    cursor.execute("insert into blogs values (null, '第二个博客', '博客内容', '2018-08-06')")
    cursor.execute("insert into blogs values (null, '第三个博客', '博客内容', '2018-08-06')")
    cursor.execute("insert into blogs values (null, '第四个博客', '博客内容', '2018-08-06')")

    #创建user表
    cursor.execute('DROP TABLE IF EXISTS user')
    cursor.execute("CREATE TABLE user(username VARCHAR(20) PRIMARY KEY, password VARCHAR(20))")
    cursor.execute("INSERT INTO user VALUES('admin', '123456')")
    conn.commit()
except BaseException as e:
    print("Error: %s" % e)